package com.own.component.common.model;

import com.own.base.BaseResponseCode;
import com.own.component.common.em.RequestEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

/**
 * ResultModel
 *
 * @author chenxueli
 * @date 2024-01-18 15:28:18
 */
@Schema(title = "返回结果类")
@Getter
@Setter
@Accessors(chain = true)
@ToString
public final class ResultModel<T> extends BaseResponseModel {

    @Schema(title = "响应结果")
    private T data;

    public ResultModel(BaseResponseCode responseCode) {
        super(responseCode);
    }

    public ResultModel(BaseResponseCode responseCode, T t) {
        super(responseCode);
        this.data = t;
    }

    public ResultModel() {
        this(RequestEnum.SUCCESS);
    }

    public ResultModel(T t) {
        this(RequestEnum.SUCCESS, t);
    }

    public ResultModel(T t, String message) {
        this(t);
        setMessage(message);
    }

    public ResultModel(Boolean flag, T t) {
        if (flag) {
            super.responseCode(RequestEnum.FAILURE);
        } else {
            super.responseCode(RequestEnum.SUCCESS);
            this.data = t;
        }
    }

    public ResultModel(UpdateModel<T> model) {
        if (model.getFlag()) {
            super.responseCode(RequestEnum.SUCCESS);
            this.data = model.getData();
        } else {
            if (StringUtils.isBlank(model.getMessage())) {
                super.responseCode(RequestEnum.OPERATION_FAILURE);
            } else {
                super.content("00001", model.getMessage());
            }
        }
    }

    public T data() {
        return data;
    }

}
